package com.android.controller;

import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * Word List Class - Creates a word list from a given text file.
 * 
 * @author David Alban
 * @version 1.0 
 */
public class WordList
{
	/**
	 * String representation of the word to be drawn.
	 */
	private String word;
	/**
	 * Part of speech of word 
	 * 
	 * Adjective		describes a noun
	 * Adverb			describes a verb, adjective or adverb
	 * Conjunction		joins clauses or sentences or words
	 * Interjection		short exclamation, sometimes inserted into a sentence
	 * Noun				thing or person
	 * Preposition		links a noun to another word
	 * Pronoun			replaces a noun
	 * Verb				action or state
	 */ 
	private String wordType;
	/**
	 * String representation of the first hint on the word 
	 */
	private String hint1;
	/**
	 * String representation of the second hint on the word 
	 */
	private String hint2;
	/**
	 * Reference to an array of word sets
	 */	
	private WordSet [] wordList;
	/**
	 * Number of words in the word list file
	 */	
	private int listSize;
	/**
	 * Reference to the random number generator
	 */	
	private RandomNumberGenerator randomNumGen;
	/**
	 * Uniformly distributed random number
	 */	
	private double randomNum;
	/**
	 * Word list index
	 */	
	private int index;
	/**
	 * Word list file name
	 */
	public static final String FILE_NAME = "word_list.txt";
	/**
	 * Maximum number of words the word list file can have
	 */
	public static final int MAX_LIST_SIZE = 1000;
	
	/**
	 * Constructor - Initializes the input class.
	 */	
	public WordList()
	{
		word = "";
		wordType = "";
		hint1 = "";
		hint2 = "";
		wordList = new WordSet [MAX_LIST_SIZE];
		listSize = 0;
		randomNumGen = new RandomNumberGenerator();
		randomNum = 0;
		index = 0;
		
	    String line;
	    BufferedReader input = null;
	    StringTokenizer token;

	    // Open word list file 
	    try 
	    {
	    	input = new BufferedReader(new FileReader(FILE_NAME));
	    }
	    // IOException occurs occurs when trying to access the word list file
	    catch (IOException event) 
	    {	    	
	    	throw new IllegalArgumentException(FILE_NAME + " cannot be " +
	    			"accessed.");          
	    }  
    
		if(input != null)
		{		
			try
			{
				line = input.readLine();

				while((line != null) && (listSize < MAX_LIST_SIZE))
				{
					token = new StringTokenizer(line,":");
					
					while(token.hasMoreTokens())
					{
						word = token.nextToken();
						
						wordType = token.nextToken();
						hint1 = token.nextToken();
						hint2 = token.nextToken();
					}
					
					// Initialize the word list array
					wordList[listSize] = new WordSet(word,wordType,hint1,hint2);
					
					line = input.readLine();
					
					listSize++;
				}
			}
			
			catch(IOException event)
			{
		    	throw new IllegalArgumentException("Line " + listSize + 
		    			" cannot be accessed."); 				
			}
			
			// Close word list file
			try
			{
				input.close();
			}
			catch(IOException event)
			{
		    	throw new IllegalArgumentException(FILE_NAME + " cannot be " +
		    			"closed.");			
			}
		}
	}
	
	/**
	 * Accessor Method - Returns word to be drawn, using a given index.
	 * @param	Index		Word list index
	 * @return	word		Word to be drawn
	 */
	public String getWord(int Index)
	{
		index = Index;
		
		if(index < listSize)
		{
			word = wordList[index].getWord();
		}
		else
		{
			word = null;
		}
		
		return word;
	}
	
	/**
	 * Accessor Method - Returns word to be drawn, using a random index.
	 * @return	word		Word to be drawn
	 */
	public String getWord()
	{
		randomNumGen.setRand0(randomNumGen.getSeed());
		randomNum = randomNumGen.getRand0()*listSize;
		index = (int)randomNum;
		
		word = wordList[index].getWord();
		
		return word;
	}
	
	/**
	 * Accessor Method - Returns the word type, using current index.
	 * @return	WordType	Word type
	 */
	public String getWordType()
	{
		wordType = wordList[index].getWordType();
		
		return wordType;
	}	
	/**
	 * Accessor Method - Returns the word type, using a given index.
	 * @param	Index		Word list index
	 * @return	WordType	Word type
	 */
	public String getWordType(int Index)
	{
		wordType = wordList[Index].getWordType();
		
		return wordType;
	}
	
	/**
	 * Accessor Method - Returns first hint on the word, using current index.
	 * @return	hint1		First hint on the word
	 */
	public String getHint1()
	{
		hint1 = wordList[index].getHint1();
		
		return hint1;
	}
	
	/**
	 * Accessor Method - Returns first hint on the word, using a given index.
	 * @param	Index		Word list index
	 * @return	hint1		First hint on the word
	 */
	public String getHint1(int Index)
	{
		hint1 = wordList[Index].getHint1();
		
		return hint1;
	}
	
	/**
	 * Accessor Method - Returns second hint on the word, using current index.
	 * @return	hint2		Second hint on the word
	 */
	public String getHint2()
	{
		hint2 = wordList[index].getHint2();
		
		return hint2;
	}
	
	/**
	 * Accessor Method - Returns second hint on the word, using a given index.
	 * @param	Index		Word list index
	 * @return	hint2		Second hint on the word
	 */
	public String getHint2(int Index)
	{
		hint2 = wordList[Index].getHint2();
		
		return hint2;
	}
	
	/**
	 * This method provides unit testing of the methods of this class.
	 * @param args		Command line arguments
	 */
	public static void main (String []args)
	{
		WordList list = new WordList();
		
		for(int i=0; i<list.listSize; i++)
		{
			System.out.println("----------------------------------------");
			System.out.println("Word  :    " + list.getWord());
			System.out.println("Word Type: " + list.getWordType());
			System.out.println("Hint 1:    " + list.getHint1());
			System.out.println("Hint 2:    " + list.getHint2());
		}
	}
}
